$(function(){
	$('#tt').datagrid({
		url:'/bookGroup/getListPage'
	});
	
	$.get('/book/revert/time/get',function(data){
		if (data.status == '1') {
			$("#time").datebox("setValue",data.data);  
		} else {
			$.messager.alert(market.content.title, data.message);
		}
    });
});

function toAdd(){
	$('#addForm').form('clear');
	$('#name').validatebox({ 
		required:true 
	});
	$('#maxFail').val(4);
	$('#status').combobox('select',1);
	$('#addWin').window('open');
}

function toEdit(){
	var rows = $("#tt").datagrid("getSelections");
    if (rows&&rows.length>1){
    	$.messager.alert("提示", "请先选择一条记录");
		return;
    }
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	$('#idId').val(record.id);
	$('#name').val(record.name);
	$('#maxFail').val(record.maxFail);
	$('#status').combobox('select',record.status);
	
	$('#name').validatebox({ 
		required:false 
	});
	
	$('#addWin').window('open');
}

function toDel(){
	var rows = $("#tt").datagrid("getSelections");
    if (rows&&rows.length>0){
    	var arrayId = [];
    	for(var i=0;i<rows.length;i++){
    		arrayId.push(rows[i].id);
    	}
    	var ids = arrayId.join(",");
	    showConfirm(market.content.title, market.content.confirm, function(){
		  $.post('/bookGroup/delete',{ids:ids}, function(data) {
		  	if (data.return_code == '1') {
				top.showMsg(market.content.title, data.return_msg, alert);
				$("#tt").datagrid('reload');
				$('#tt').datagrid('clearSelections');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		  });
	   });
    } else {
    	showMsg(market.content.title, market.content.tips, alert);
    }
}

function doSearch(){
	var sname=$('#sname').val();
	$('#tt').datagrid({
		url:'/bookGroup/getListPage',
		queryParams:{
			name:sname
		}
	});
	$('#tt').datagrid('unselectAll');
}

function save(){
	var id=$('#idId').val();
	var url='/bookGroup/update';
	if(id==null||id==''){
		url='/bookGroup/save';
	}
	
	$('#addForm').form('submit',{
		url : url,
		onSubmit : function() {
			var flag = $(this).form('validate');
			if (!flag) {
				return flag;
			}
			showProcess(true, market.content.title, market.content.submitting);
			return true;
		},
		success : function(data) {
			showProcess(false);
			var data = eval('(' + data + ')');
			if (data.return_code == '1') {
				$("#addWin").window('close');
				$("#tt").datagrid('reload');
				$('#tt').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		onLoadError : function() {
			showProcess(false);
			$.messager.alert(market.content.title, market.content.networkError);
		}
	});
}

function cancel(){
	$('#addWin').window('close');
}

function formatStatus(value){
	if(value==1){
		return '启用';
	}else{
		return '关闭';
	}
}

function showBookWin(){
	var rows = $("#tt").datagrid("getSelections");
    if (rows&&rows.length>1){
    	$.messager.alert("提示", "请先选择一条记录");
		return;
    }
    
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	var id=record.id;
	$('#selectWin').window('open');
	$('#bookGroupId').val(id);
	$('#bookName').val('');
	
	$('#tbBook').datagrid({
		url:'/book/getListPage?bookGroupId='+id,
	});
}

function searchBook(){
	var name=$('#bookName').val();
	var bookGroupId=$('#bookGroupId').val();
	$('#tbBook').datagrid({
		queryParams:{
			name:name,
			bookGroupId:bookGroupId
		}
	});
}

function toAddBook(){
	$('#addBookForm').form('clear');
	var groupId  = $('#bookGroupId').val();
	$('#groupId').val(groupId);
//	$('#status').combobox('select',1);
	$('#addBookWin').window('open');
}

function toEditBook(){
	var rows = $("#tbBook").datagrid("getSelections");
    if (rows&&rows.length>1){
    	$.messager.alert("提示", "请先选择一条记录");
		return;
    }
	var record = $('#tbBook').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	$('#bookId').val(record.id);
	$('#nameAdd').val(record.name);
	$('#chapter').val(record.chapter);
	$('#totalMoney').val(record.totalMoney);
	
	var groupId  = $('#bookGroupId').val();
	$('#groupId').val(groupId);
	
	$('#mark').validatebox({ 
		required:false 
	});
	
	$('#addBookWin').window('open');
}

function saveBook(){
	var id=$('#bookId').val();
	var url='/book/update';
	if(id==null||id==''){
		url='/book/save';
	}
	
	$('#addBookForm').form('submit',{
		url : url,
		onSubmit : function() {
			var flag = $(this).form('validate');
			if (!flag) {
				return flag;
			}
			showProcess(true, market.content.title, market.content.submitting);
			return true;
		},
		success : function(data) {
			showProcess(false);
			var data = eval('(' + data + ')');
			if (data.return_code == '1') {
				$("#addBookWin").window('close');
				$("#tbBook").datagrid('reload');
				$('#tbBook').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		onLoadError : function() {
			showProcess(false);
			$.messager.alert(market.content.title, market.content.networkError);
		}
	});
}

function deleteBook(){
	var rows = $("#tbBook").datagrid("getSelections");
    if (rows&&rows.length>0){
    	var arrayId = [];
    	for(var i=0;i<rows.length;i++){
    		arrayId.push(rows[i].id);
    	}
    	var ids = arrayId.join(",");
	    showConfirm(market.content.title, market.content.confirm, function(){
		  $.post('/book/delete',{ids:ids}, function(data) {
		  	if (data.return_code == '1') {
				top.showMsg(market.content.title, data.return_msg, alert);
				$("#tbBook").datagrid('reload');
				$('#tbBook').datagrid('clearSelections');
//				$('#tbBook').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		  });
	   });
    } else {
    	showMsg(market.content.title, market.content.tips, alert);
    }
}

function cancelBook(){
	$('#addBookWin').window('close');
}

function doImport() {
	$('#uploadForm').form(
	    'submit',
	    {
			url : '/book/import',
			onSubmit : function() {
				var excelFile = $('#excelFile').val();
				if (excelFile.lastIndexOf('.xls') < 0&&excelFile.lastIndexOf('.xlsx') < 0) {
					top.showMsg(market.content.title, '上传文件必须为excel文件', alert);
					return false;
				}
				return $(this).form('enableValidation').form('validate');
			},
			success : function(data) {
				showProcess(false);
				var data = eval("(" + data + ")");
				if (data.status == '1') {
					$('#counter').val(data.data);
					$('#showWin').window('open');
					$('#tbExcel').datagrid({
						url:'/book/getTempPage',
						queryParams:{
							counter:data.data
						}
					});
				} else {
					$.messager.alert(market.content.title, data.message);
				}
				$('#excelFile').val('');
			},
			onLoadError : function() {
				showProcess(false);
				$.messager.alert(market.content.title, market.content.networkError);
			}
		}); 
}

function doUpdate(){
	var counter = $('#counter').val();
	var bookGroupId = $('#bookGroupId').val();
    showConfirm(market.content.title, "确定更新入库吗？", function(){
      showProcess(true);
	  $.post('/book/doImport',{counter:counter,bookGroupId:bookGroupId}, function(data) {
		showProcess(false);
	  	if (data.return_code == '1') {
			top.showMsg(market.content.title, data.return_msg, alert);
			$('#showWin').window('close');
			$("#tbBook").datagrid('reload');
			$('#tbBook').datagrid('clearSelections');
		} else {
			$.messager.alert(market.content.title, data.return_msg);
		}
	  });
   });
}

function doExport() {
	var sname=$('#bookName').val();
	var groupId=$('#bookGroupId').val();
	$('#ff').form(
	'submit',
	{
		url : '/book/export',
		onSubmit : function() {
			$("#ename").val(sname);
			$("#eGroupId").val(groupId);
		},
		success : function(data) {
			showProcess(false);
			var data = eval('(' + data + ')');
			if (data.return_code == '1') {
				top.showMsg(market.content.title, data.return_msg, alert);
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		onLoadError : function() {
			showProcess(false);
			$.messager.alert(market.content.title, market.content.networkError);
		}
	});
}

function doRevert(){
    showConfirm(market.content.title, "确定把已支付金额清零吗？", function(){
	  $.post('/book/revert',function(a) {
		var data = eval(a);
		if (data.status == '1') {
			top.showMsg(market.content.title, "成功恢复"+data.data+"条数据", alert);
//			$("#tt").datagrid('reload');
//			$('#tt').datagrid('clearSelections');
		}else {
			$.messager.alert(market.content.title, data.message);
		}
	  	
	  });
   });
}

function updateTime(){
	showConfirm(market.content.title, "确定更新清零时间吗？", function(){
	  var time = $('#time').datebox('getValue');
	  
	  var nowDate = new Date();
	  var date = new Date(time);
	  if(date<nowDate){
		  alert("清零时间必须大于当前时间");
		  return;
	  }
	  $.post('/book/revert/time/set',{time:time},function(data) {
		if (data.status == '1') {
			top.showMsg(market.content.title, data.message, alert);
			$("#time").val(data.data);
		} else {
			$.messager.alert(market.content.title, data.message);
		}
	  	
	  });
   });
}
